package xj.graph2d.graph;

import java.awt.geom.Point2D;
import java.util.Iterator;

public class GraphSimpleLayout implements GraphLayoutManager {

  public GraphSimpleLayout(Graph g) {
    graph = g;
  }

  public void doLayout() {
    layoutEdges();
  }

  public void layoutEdges() {
    if (graph != null) {
      Iterator edges = graph.edges();
      while (edges.hasNext()) {
	GraphEdge edge = (GraphEdge) edges.next();
	if (edge != null) {
	  edge.adjustEnds();
	}
      }
    }
  }

  protected Graph graph;

  public static void moveNodeTo(GraphNode node, double x, double y) {
    if (node != null) {
      Point2D p = node.getCenter();
      if (p != null) {
	double dx = x - p.getX();
	double dy = y - p.getY();
	node.move(dx, dy);
      }
    }
  }

  protected void setTopLeftMargin(double top, double left) {
    if (graph != null) {
      double minx = Integer.MAX_VALUE;
      double miny = Integer.MAX_VALUE;
      Iterator nodes = graph.nodes();
      while (nodes.hasNext()) {
	GraphNode n = (GraphNode) nodes.next();
	Point2D p = n.getULCorner();
	minx = Math.min(minx, p.getX());
	miny = Math.min(miny, p.getY());
      }
      double dx = left - minx;
      double dy = top - miny;
      graph.move(dx, dy);
    }
  }

  static public class DoubleRef {
    public double val;

    public DoubleRef(double d) {
      val = d;
    }
  }

}
